package com.ywm.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.commonUtil.JwtUtils;
import com.ywm.commonUtil.MD5;
import com.ywm.educenter.entity.UcenterMember;
import com.ywm.educenter.entity.vo.RegisterVo;
import com.ywm.educenter.mapper.UcenterMemberMapper;
import com.ywm.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author maDondMei
 * @since 2020-05-25
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //用户登录
    @Override
    public String login(UcenterMember user) throws Exception {
        //获取登录的手机号和密码
        String mobile = user.getMobile();
        String password = user.getPassword();
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)) {
            throw new Exception("");
        }
        //判断手机号
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember==null) {
            //没有手机号
            throw new Exception("没有手机号");
        }
        //判断密码
        //存储到数据库的密码是加密的，我们需要对输入的加密后和数据库中的密码做比较
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new Exception("密码错误");
        }
        //判断用户是否禁用
        if (mobileMember.getIsDisabled()) {
            throw new Exception("用户被禁用");
        }
        //登录成功生成token(使用jwt工具类)
//        System.out.println(mobileMember.getId());
//        System.out.println(mobileMember.getNickname());
        return JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
    }
    //注册用户
    @Override
    public void register(RegisterVo registerVo) throws Exception {
        //获取注册数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //信息非空判断
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)||StringUtils.isEmpty(nickname)) {
            System.out.println("注册失败");
            throw new Exception("有空值，注册失败");
        }
        //判断验证码
        String redisCode = redisTemplate.opsForValue().get("phone");
        System.out.println("redis:"+redisCode);
        System.out.println("code:"+redisCode);
//        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new Exception("验证码错误");
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",registerVo.getMobile());
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){throw new Exception("验证码错误");}
        //添加到数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://ywm-guli.oss-cn-heyuan.aliyuncs.com/2020/05/19/5a25900d92ad44619cadebf45fab8589file.png");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMenber(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if (member == null) {
            return null;
        }
        return member;
    }
    //查询某一天的注册人数
    @Override
    public int countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
